package com.briup.estore.common.service.impl;

import com.briup.estore.common.bean.Category;
import com.briup.estore.common.bean.VO.CategoryVO;
import com.briup.estore.common.dao.ICategoryDao;
import com.briup.estore.common.dao.IShopDao;
import com.briup.estore.common.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author adam
 * @date 2022/10/13
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private ICategoryDao categoryDao;
    @Autowired
    private IShopDao shopDao;

    @Override
    public List<CategoryVO> findAll() throws Exception {
        List<CategoryVO> list = new ArrayList<>();
        List<Category> categoryList = findOneLevel();
        for (Category category : categoryList) {
            CategoryVO vo = new CategoryVO();
            vo.setId(category.getId());
            vo.setName(category.getName());
            List<Category> categorys = categoryDao.findByParentId(category.getId());
            vo.setCategories(categorys);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<Category> findOneLevel() throws Exception {
        List<Category> list = categoryDao.findByParentIdIsNull();
        if (list.isEmpty()) {
            throw new Exception("暂无数据");
        }
        return list;
    }

    @Override
    public List<Category> findTwoLevel(long oneLevelCategoryId) throws Exception {
        List<Category> list = categoryDao.findByParentId(oneLevelCategoryId);
        if (list.isEmpty()) {
            throw new Exception("暂无数据");
        }
        return list;
    }


    @Override
    public void addCategory(Category category) throws Exception {
        categoryDao.save(category);
    }

    @Override
    public void delete(long id) {
        Optional<Category> optional = categoryDao.findById(id);
        Category category = optional.get();
        if (category.getParentId() == null) {
            List<Category> list = categoryDao.findByParentId(id);
            categoryDao.deleteAll(list);
        }
        categoryDao.delete(category);
    }

    @Override
    public List<Category> findOneLevelByName(String name) {
        List<Category> list = categoryDao.findByNameAndParentIdIsNull(name);
        return list;
    }

    @Override
    public List<Category> findTwoLevelByName(String name, long id) {
        List<Category> list = categoryDao.findByNameAndParentId(name, id);
        return list;
    }

    @Override
    public long countTwoLevelCategory() {
        long count = categoryDao.count(Example.of(Category.builder().parentId(null).build()));
        return count;
    }

    @Override
    public Map<String, Long> categoryStats() {
        List<String[]> stats = shopDao.categoryStats();
        HashMap<String, Long> map = new HashMap<>();
        for (String[] stat : stats) {
          map.put(stat[0],Long.parseLong(stat[1]));
        }
        return map;
    }


}
